package com.lemote.action.order;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.lemote.action.BaseAction;
import com.lemote.core.PropertyFilter;
import com.lemote.entity.CircuitBoard;
import com.lemote.entity.FailReport;
import com.lemote.entity.FlowTask;
import com.lemote.entity.RepairReport;
import com.lemote.entity.ShowEntity;
import com.lemote.entity.User;
import com.lemote.exception.AppException;
import com.lemote.service.order.CircuitBoardService;
import com.lemote.service.order.FailReportService;
import com.lemote.service.order.RepairReportService;
import com.lemote.service.order.ShowEntityService;
import com.lemote.utils.Constant;

/**
 * 
 * @author slaton
 * @time 2011-7-16 上午10:47:04
 * @Version:0.0.1
 * @since smt
 */
@Service("repairReportAction")
public class RepairReportAction extends
		BaseAction<RepairReport, RepairReportService> {

	private RepairReport report;
	
	private CircuitBoard circuitBoard;

	private CircuitBoardService circuitBoardService;
	
	private ShowEntityService showEntityService;
	
	private FailReportService failReportService;

	public RepairReport getReport() {
		return report;
	}

	public void setReport(RepairReport report) {
		this.report = report;
	}

	@Override
	public RepairReport getModel() {

		return report;
	}

	@Override
	public String list() throws AppException {

		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(request);
		page = entityService.search(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws AppException {

		return null;
	}
	
	public String analyze(){
		setBackUrl("board/analyzelist.htm");
		String bid = request.getParameter("bid");
		report.setId(null);
		if (report != null && bid != null && !"".equals(StringUtils.trim(bid))) {
			User user = (User) request.getSession().getAttribute(Constant.SESSION_USER);
			report.setRepairDate(new Date());
			report.setAnalyzer(user);
			Long cid = Long.parseLong(bid);
			CircuitBoard board = circuitBoardService.get(cid);
			report.setBoard(board);
			report.setJobName(board.getPreTask().getTaskName());
			report.setStatus(1);
			board.setAnalyzes(1);
			entityService.add(report);
			List<RepairReport> repairs = board.getRepairs();
			if (repairs==null) {
				repairs = new ArrayList<RepairReport>();
			}
			repairs.add(report);
			board.setRepairs(repairs);
			board.setStatus(report.getResult());
			circuitBoardService.update(board);
			
			return GLOBALSUCCESS;
		}
		return ERROR;
	}
	
	public String toRepair(){
		String cid = request.getParameter("cid");
		if (cid!=null&&!"".equals(cid)) {
			this.circuitBoard = circuitBoardService.get(Long.parseLong(cid));
			if (circuitBoard!=null&&("top_aoi_test".equals(circuitBoard.getPreTask().getTaskEname())||"bot_aoi_test".equals(circuitBoard.getPreTask().getTaskEname()))) {
				List<FailReport> fails = getFailReportByBoard(cid);
				this.report = new RepairReport();
				StringBuffer location = new StringBuffer();
				StringBuffer code = new StringBuffer();
				for (FailReport fr : fails) {
					location.append(fr.getLocation());
					location.append(", ");
					code.append(fr.getErrorCode());
					code.append(", ");
				}
				report.setLocation(location.toString());
				report.setErrorCode(code.toString());
				return "aoiRepair";
			}
			List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
			PropertyFilter filter1 = new PropertyFilter("EQL_board.id",cid); 
			PropertyFilter filter2 = new PropertyFilter("EQI_status","1"); 
			filters.add(filter1);
			filters.add(filter2);
			List<RepairReport> reports = entityService.getList(filters);
			if (reports!=null&&reports.size()>0) {
				this.report = reports.get(0);
			}
		}
		return SUCCESS;
	}

	private List<FailReport> getFailReportByBoard(String cid) {
		List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
		PropertyFilter filter1 = new PropertyFilter("EQL_board.id", cid);
		PropertyFilter filter2 = new PropertyFilter("EQI_status", "1");
		filters.add(filter1);
		filters.add(filter2);
		List<FailReport> fails = failReportService.getList(filters);
		return fails;
	}

	@Override
	public String save() throws AppException {
		setBackUrl("board/repairList.htm");
		String rid = request.getParameter("rid");
		User user = (User) request.getSession().getAttribute(Constant.SESSION_USER);
		if (rid != null && !"".equals(StringUtils.trim(rid))) {
			String  process = request.getParameter("process");
			String  result = request.getParameter("result");
			RepairReport rr = entityService.get(Long.parseLong(rid));
			rr.setRepairDate(new Date());
			rr.setRepairer(user);
			rr.setProcess(process);
			rr.setResult(result);
			rr.setStatus(0);
			entityService.update(rr);
			CircuitBoard board = rr.getBoard();
			board.setAnalyzes(0);
			FlowTask task = board.getPreTask();
			board.setPreTask(board.getFlowTask());
			board.setFlowTask(task);
			List<RepairReport> repairs = board.getRepairs();
			if (repairs==null) {
				repairs = new ArrayList<RepairReport>();
			}
			repairs.add(rr);
			board.setRepairs(repairs);
			board.setStatus(report.getResult());
			circuitBoardService.update(board);
			return GLOBALSUCCESS;
		}else{
			String boardid = request.getParameter("boardid");
			if (boardid!=null&&!"".equals(boardid)&&report!=null) {
				CircuitBoard board = circuitBoardService.get(Long.parseLong(boardid));
				report.setRepairer(user);
				report.setBoard(board);
				report.setJobName(board.getPreTask().getTaskEname());
				report.setRepairDate(new Date());
				report.setStatus(1);
				entityService.add(report);
				List<FailReport> fails = getFailReportByBoard(boardid);
				for (FailReport f : fails) {
					f.setStatus(0);
					failReportService.update(f);
				}
				FlowTask task = board.getPreTask();
				board.setAnalyzes(0);
				board.setPreTask(board.getFlowTask());
				board.setFlowTask(task);
				List<RepairReport> repairs = board.getRepairs();
				if (repairs==null) {
					repairs = new ArrayList<RepairReport>();
				}
				repairs.add(report);
				board.setRepairs(repairs);
				board.setAnalyzes(0);
				board.setStatus(report.getResult());
				circuitBoardService.update(board);
				return GLOBALSUCCESS;
				
			}
		}
		return ERROR;
	}

	@Override
	public String delete() throws AppException {

		return null;
	}

	@Override
	public void prepareModel() throws AppException {

		if (id != null) {
			report = entityService.get(id);
		} else {
			report = new RepairReport();
		}

	}

	@Autowired
	@Override
	public void setEntityService(RepairReportService entityService) {

		this.entityService = entityService;
	}

	@Autowired
	public void setCircuitBoardService(CircuitBoardService circuitBoardService) {
		this.circuitBoardService = circuitBoardService;
	}

	@Autowired
	public void setShowEntityService(ShowEntityService showEntityService) {
		this.showEntityService = showEntityService;
	}

	public CircuitBoard getCircuitBoard() {
		return circuitBoard;
	}

	@Autowired
	public void setFailReportService(FailReportService failReportService) {
		this.failReportService = failReportService;
	}
	
	

}
